package cn.edu.tju.elm.model.DTO;

import cn.edu.tju.elm.common.constants.TransactionType;
import cn.edu.tju.elm.model.entity.WalletTransaction;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

import java.math.BigDecimal;
import java.time.LocalDateTime;

@Data
@SuperBuilder
@AllArgsConstructor
@NoArgsConstructor
public class WalletTransactionDTO {
    private Long id;
    private LocalDateTime transactionTime;
    private BigDecimal amount;
    private TransactionType transactionType;
    private String remark;
    private LocalDateTime createTime;

    // fromWallet 相关信息
    private Long fromWalletId;
    private String fromWalletUserName;
    private BigDecimal fromWalletBalance;

    // toWallet 相关信息
    private Long toWalletId;
    private String toWalletUserName;
    private BigDecimal toWalletBalance;

    // order 相关信息（如果需要）
    private Long orderId;

    // 在 WalletTransaction.java 中添加这个方法
    public static WalletTransactionDTO convertToDTO(WalletTransaction transaction) {
        if (transaction == null) {
            return null;
        }

        return WalletTransactionDTO.builder()
                .id(transaction.getId())
                .transactionTime(transaction.getTransactionTime())
                .amount(transaction.getAmount())
                .transactionType(transaction.getTransactionType())
                .remark(transaction.getRemark())
                .createTime(transaction.getCreateTime())

                // fromWallet 信息
                .fromWalletId(transaction.getFromWallet() != null ? transaction.getFromWallet().getId() : null)
                .fromWalletUserName(transaction.getFromWallet() != null && transaction.getFromWallet().getUser() != null ?
                        transaction.getFromWallet().getUser().getUsername() : null) // 根据你的 User 实体字段调整
                .fromWalletBalance(transaction.getFromWallet() != null ? transaction.getFromWallet().getBalance() : null)

                // toWallet 信息
                .toWalletId(transaction.getToWallet() != null ? transaction.getToWallet().getId() : null)
                .toWalletUserName(transaction.getToWallet() != null && transaction.getToWallet().getUser() != null ?
                        transaction.getToWallet().getUser().getUsername() : null) // 根据你的 User 实体字段调整
                .toWalletBalance(transaction.getToWallet() != null ? transaction.getToWallet().getBalance() : null)

                // order 信息
                .orderId(transaction.getOrder() != null ? transaction.getOrder().getId() : null)

                .build();
    }
}